using UnityEngine;
using System.Collections;

public class BuildingSpawnManager : MonoBehaviour {

	public Transform[] buildingsPatterns;
    public SantaClaus player;
    private Transform lastPattern;
    private float addSpeed;

    void Start()
    {
        if (GameObject.Find("SantaClaus"))
        {
            player = GameObject.Find("SantaClaus").GetComponent<SantaClaus>();
        }

        addSpeed = 0;
        Instantiate();
    }
	
	void Update() 
	{
        if (lastPattern.GetComponent<PatternFather>().last.position.x <= -10)
        {
            Instantiate();
        }
		
	}
	
	void Instantiate()
	{
        addSpeed++;

        player.gravity += 0.01f;

		int random = Random.Range(0, buildingsPatterns.Length);

        lastPattern = Instantiate(buildingsPatterns[random], buildingsPatterns[random].transform.position, Quaternion.identity) as Transform;
        lastPattern.GetComponent<PatternFather>().setSpeed(addSpeed/5);
	}
}